package com.gxa.service.impl;

import com.gxa.common.Contract_statusConstants;
import com.gxa.common.JsonResult;
import com.gxa.common.R;
import com.gxa.mapper.ContractMapper;
import com.gxa.pojo.Contract;
import com.gxa.pojo.Contract_status;
import com.gxa.service.ContractService;
import com.gxa.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ContractServiceImpl implements ContractService{

    @Autowired(required = false)
    private ContractMapper contractMapper;

//    12.06
    @Override
    public JsonResult queryContractMap(Integer page, Integer limit) {

        JsonResult jsonResult = new JsonResult();

        Integer param1 = (page - 1)*limit;
        Integer param2 = limit;
        List<Map> contractMap = contractMapper.queryContractMap(param1,param2);
        int result = contractMapper.countContract();
        jsonResult.setCount(result);
        jsonResult.setData(contractMap);
        jsonResult.setCode(200);
        jsonResult.setMsg("查询成功");

        return jsonResult;
    }

    @Override
    public R searchContractByEntity(Contract contract) {
        return null;
    }

//    12.08
    @Override
    public R searchContractByName(String contract_search) {
        R r = new R();
        int result = contractMapper.countContractByPhone(contract_search);
        if (result > 0) {
            List<Contract> contractList = contractMapper.searchContractByPhone(contract_search);
            r.setMsg("查询成功");
            r.setCode(200);
            r.setData(contractList);
            return r;

        } else {
            result = contractMapper.countContractByNo(contract_search);
            if (result > 0) {
                List<Contract> contractList = contractMapper.searchContractByNo(contract_search);
                r.setMsg("查询成功");
                r.setCode(200);
                r.setData(contractList);
                return r;
            } else {
                result = contractMapper.countContractByName(contract_search);
                if (result > 0) {
                    List<Contract> contractList = contractMapper.searchContractByName(contract_search);
                    r.setMsg("查询成功");
                    r.setCode(200);
                    r.setData(contractList);
                    return r;
                }else {
                    r.setMsg("查询失败");
                    r.setCode(400);
                    return r;
                }
                }
            }

        }


//    12.06
    @Override
    public R insertNewContractList(Contract contract) {

        R r = new R();

            int result = contractMapper.insertNewContractList(contract);
            int result2 = contractMapper.insertContractStatusList(contract);
            if (result>0 && result2 >0){

                r.setCode(200);
                r.setMsg("添加成功！");
            }
        return r;

    }

    @Override
    public R updateContractStatusList(Contract_status contract_status) {
        R r = new R();
//        Integer ID = contract_status.getContract_id();
       int result = contractMapper.updateContractStatusList(contract_status);
//       if(result > 0){
//           Map map = contractMapper.queryContractStatusMap(ID);
           r.setCode(200);
           r.setData(result);
           r.setMsg("审核成功！");

//       }


        return r;
    }


//    12.07
    @Override
    public R deleteContractByNo(String contract_no) {
        R r = new R();
        int result = contractMapper.deleteContractByNo(contract_no);
        if (result>0){
            r.setCode(200);
            r.setData(result);
            r.setMsg("删除成功！");
        }
        else {
            r.setCode(400);
            r.setMsg("删除失败！");
        }
        return r;
    }

    @Override
    public R updateContractList(Contract contract) {
        R r = new R();
        int result = contractMapper.updateContractList(contract);
        if (result>0){
            r.setCode(200);
            r.setMsg("编辑成功！");
        }
        return r;
    }


//    12.08
    @Override
    public R updateContractRenewList(String contract_endday, Double contract_summoney, Double contract_receivemoney,Integer contract_id) {
        R r = new R();
        int result = contractMapper.updateContractRenewList(contract_endday,contract_summoney,contract_receivemoney,contract_id);
        if (result>0){
            r.setCode(200);
            r.setMsg("续费成功！");
            r.setData(result);
        }else{
            r.setCode(400);
            r.setMsg("续费失败！");
        }
        return r;
    }

    @Override
    public R updateContractReceive(Double contract_receivemoney,Integer contract_id) {
        R r = new R();
        int result = contractMapper.updateContractReceive(contract_receivemoney,contract_id);
        if (result>0){
            r.setCode(200);
            r.setMsg("到款成功！");
            r.setData(result);
        }else{
            r.setCode(400);
            r.setMsg("到款失败！");
        }
        return r;
    }

    @Override
    public JsonResult searchContractByStartday(Integer page, Integer limit) {
        JsonResult jsonResult = new JsonResult();

        Integer param1 = (page - 1)*limit;
        Integer param2 = limit;
        List<Map> contractMap = contractMapper.searchContractByStartday(param1,param2);
        int result = contractMapper.countContractByStartday();
        jsonResult.setCount(result);
        jsonResult.setData(contractMap);
        jsonResult.setCode(200);
        jsonResult.setMsg("查询成功");

        return jsonResult;
    }

    @Override
    public JsonResult searchContractByEndday(Integer page, Integer limit) {
        JsonResult jsonResult = new JsonResult();

        Integer param1 = (page - 1)*limit;
        Integer param2 = limit;
        List<Map> contractMap = contractMapper.searchContractByEndday(param1,param2);
        int result = contractMapper.countContractByEndday();
        jsonResult.setCount(result);
        jsonResult.setData(contractMap);
        jsonResult.setCode(200);
        jsonResult.setMsg("查询成功");

        return jsonResult;
    }

    @Override
    public JsonResult searchNewContractByWeek(Integer page, Integer limit) {
        JsonResult jsonResult = new JsonResult();

        Integer param1 = (page - 1)*limit;
        Integer param2 = limit;
        List<Map> contractMap = contractMapper.searchNewContractByWeek(param1,param2);
        int result = contractMapper.countNewContractByWeek();
        jsonResult.setCount(result);
        jsonResult.setData(contractMap);
        jsonResult.setCode(200);
        jsonResult.setMsg("查询成功");

        return jsonResult;
    }

    @Override
    public JsonResult searchDueContractByWeek(Integer page, Integer limit) {
        JsonResult jsonResult = new JsonResult();

        Integer param1 = (page - 1)*limit;
        Integer param2 = limit;
        List<Map> contractMap = contractMapper.searchDueContractByWeek(param1,param2);
        int result = contractMapper.countDueContractByWeek();
        jsonResult.setCount(result);
        jsonResult.setData(contractMap);
        jsonResult.setCode(200);
        jsonResult.setMsg("查询成功");

        return jsonResult;
    }

    @Override
    public JsonResult searchDueContractByMonth(Integer page, Integer limit) {
        JsonResult jsonResult = new JsonResult();

        Integer param1 = (page - 1)*limit;
        Integer param2 = limit;
        List<Map> contractMap = contractMapper.searchDueContractByMonth(param1,param2);
        int result = contractMapper.countDueContractByMonth();
        jsonResult.setCount(result);
        jsonResult.setData(contractMap);
        jsonResult.setCode(200);
        jsonResult.setMsg("查询成功");

        return jsonResult;
    }

    @Override
    public JsonResult searchNewContractByMonth(Integer page, Integer limit) {
        JsonResult jsonResult = new JsonResult();

        Integer param1 = (page - 1)*limit;
        Integer param2 = limit;
        List<Map> contractMap = contractMapper.searchNewContractByMonth(param1,param2);
        int result = contractMapper.countNewContractByMonth();
        jsonResult.setCount(result);
        jsonResult.setData(contractMap);
        jsonResult.setCode(200);
        jsonResult.setMsg("查询成功");

        return jsonResult;
    }

    @Override
    public R insertContractPart(String contract_newPart) {
        R r = new R();
        if(contract_newPart == null && contract_newPart.equals("") ){
            List<Contract> contractList = contractMapper.searchContractPart();

            r.setCode(200);
            r.setData(contractList);
            return r;
        }else{
            int result = contractMapper.insertContractPart(contract_newPart);

            if (result>0){
                List<Contract> contractList = contractMapper.searchContractPart();

                r.setCode(200);
                r.setData(contractList);
                return r;
            }
        }
        r.setCode(400);
        r.setMsg("删除失败！");
        return r;
    }


}
